﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Controllers;
using System.Drawing;

namespace Staionery_Inventory.Presentation.StockCard
{
    public partial class ApproveAdjVoucherUI__Reload_ : System.Web.UI.Page
    {
        ApproveAdjVoucherController appCon = new ApproveAdjVoucherController();
        String empName = ""; String role = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }
            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }


            if (!IsPostBack)
            {
                empName = (String)Session["UserName"];
                if (empName == null) {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "You must log in to use this page";
                }
                role = (String)Session["role"];
                if ((role != "Supervisor") && (role != "Head")) {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "You do not have the right to use this page";
                }
                lblEmpName.InnerText = empName;
                gvPendingVoucher.DataSource = appCon.getAllPendingVouchers(role);
                gvPendingVoucher.DataBind();
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

            int approveCount = 0; role = (String)Session["role"];
            List<int> voucherList = new List<int>();
            foreach (GridViewRow gvr in gvPendingVoucher.Rows)
            {
                if ((gvr.FindControl("cbPendingVoucher") as CheckBox).Checked)
                {
                    int adjVoucherDetailID = appCon.getAllPendingVouchers(role)[gvr.RowIndex].AdjVoucherDetailID;
                    voucherList.Add(adjVoucherDetailID);
                    approveCount += 1;
                }
            }
            if (approveCount == 0)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please tick on checkboxes to select approved voucher before submit";
            }
            else {
                lblMessage.ForeColor = Color.Black; lblEmpName.InnerText = empName;
                String result = appCon.approvePendingVoucher(voucherList, (String)Session["UserName"], DateTime.Now);
                lblMessage.Text = result;
                gvPendingVoucher.DataSource = appCon.getAllPendingVouchers(role);
                gvPendingVoucher.DataBind();
            }

        }

        protected void gvPendingVoucher_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int currentRowIndex = e.RowIndex; role = (String)Session["role"];
            int adjVoucherDetailID = appCon.getAllPendingVouchers(role)[currentRowIndex].AdjVoucherDetailID;
            String result = appCon.deletePendingVoucher(adjVoucherDetailID);
            lblMessage.Text = result;
            lblEmpName.InnerText = empName;
            gvPendingVoucher.DataSource = appCon.getAllPendingVouchers(role);
            gvPendingVoucher.DataBind();
        }
    }
}